package com.mingtu.jgspatrol.activity;

import android.content.Intent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.mingtu.common.entity.TabEntity;
import com.mingtu.common.utils.IntentUtils;
import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.adapter.TabbarAdapter;
import com.mingtu.jgspatrol.base.MyBaseActivity;
import com.mingtu.jgspatrol.databinding.ActivitySendTaskBinding;
import com.mingtu.jgspatrol.fragment.FinishedFragment;
import com.mingtu.jgspatrol.fragment.UnfinishedFragment;

import java.util.ArrayList;

public class SendTaskActivity extends MyBaseActivity<ActivitySendTaskBinding> {
    private static final String[] mTitles = {"进行中", "已完成"};
    private static final int[] mSeleIcons = {R.mipmap.icon_task_unfinish_selected, R.mipmap.icon_task_finish_selected};
    private static final int[] mNormalIcons = {R.mipmap.icon_task_unfinish_normal, R.mipmap.icon_task_finish_normal};

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private UnfinishedFragment unfinishedFragment;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_send_task;
    }

    @Override
    protected void initView() {
        setTitleBarBackground(getResources().getDrawable(R.drawable.titlebar_line_bottom));
        setModuleTitle("下发任务");
        showRightText();
        setRightText("新增");
        setRightTextColor(getResources().getColor(R.color.colorPrimary));
    }

    @Override
    protected void initData() {
        unfinishedFragment = new UnfinishedFragment();
        mFragments.add(unfinishedFragment);
        mFragments.add(new FinishedFragment());

        binding.viewPager.setOffscreenPageLimit(2);//需要设置此值
        binding.viewPager.setAdapter(new TabbarAdapter(getSupportFragmentManager(), mFragments));
        binding.viewPager.addOnPageChangeListener(onPageChangeListener);
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mSeleIcons[i], mNormalIcons[i]));
        }

        binding.tabNav.setTabData(mTabEntities);
        binding.tabNav.setOnTabSelectListener(onTabSelectListener);
    }

    @Override
    protected void getData() {

    }

    @Override
    protected void onClickTitlebarRightText(View view) {
        IntentUtils.getInstance().readyGoForResult(SendTaskActivity.this, SendTaskAddActivity.class, 100);
        super.onClickTitlebarRightText(view);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100) {
            if (resultCode == 200) {
                //添加成功之后 返回刷新未完成列表的数据
                try {
                    unfinishedFragment.refresh();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
    }


    private OnTabSelectListener onTabSelectListener = new OnTabSelectListener() {
        @Override
        public void onTabSelect(int position) {
            binding.viewPager.setCurrentItem(position, false);
        }

        @Override
        public void onTabReselect(int position) {

        }
    };

    private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            binding.tabNav.setCurrentTab(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };


}